package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.Person;
import com.soft.sitegenerator.business.hibernate.Profile;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.UserEditForm;

public class UserEditAction extends DispatchAction {

	public ActionForward editUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserEditForm userEditForm = (UserEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		User user = userEditForm.getUser();
		user.setId(id);

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Component component = new DataBaseComponent();
		Profile profile = (Profile) component.getBean("profilesHib");
		userEditForm.setProfiles(profile.findAll());
		userEditForm.setIdProfile(user.getProfile().getId());

		Component component2 = new DataBaseComponent();
		Person person = (Person) component2.getBean("personsHib");
		userEditForm.setPersons(person.findAll());
		userEditForm.setIdPerson(user.getPerson().getId());

		return mapping.findForward("showEdit");
	}

	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserEditForm userEditForm = (UserEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		userEditForm.getUser().setId(id);
		userEditForm.getUser().delete();

		return mapping.findForward("showList");
	}

	public ActionForward addUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		UserEditForm userEditForm = (UserEditForm) form;

		Component component = new DataBaseComponent();
		Profile profile = (Profile) component.getBean("profilesHib");
		userEditForm.setProfiles(profile.findAll());

		Component component2 = new DataBaseComponent();
		Person person = (Person) component2.getBean("personsHib");
		userEditForm.setPersons(person.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward saveUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserEditForm userEditForm = (UserEditForm) form;

		Component component = new DataBaseComponent();
		Profile profile = (Profile) component.getBean("profilesHib");

		profile.setId(userEditForm.getIdProfile());

		try {
			profile.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Component component2 = new DataBaseComponent();
		Person person = (Person) component2.getBean("personsHib");

		person.setId(userEditForm.getIdPerson());

		try {
			person.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		User user = userEditForm.getUser();
		user.setProfile(profile);
		user.setPerson(person);
		user.save();

		return mapping.findForward("showList");

	}

	public ActionForward updateUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserEditForm userEditForm = (UserEditForm) form;

		Component component = new DataBaseComponent();
		Profile profile = (Profile) component.getBean("profilesHib");
		profile.setId(userEditForm.getIdProfile());

		try {
			profile.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Component component2 = new DataBaseComponent();
		Person person = (Person) component2.getBean("personsHib");
		person.setId(userEditForm.getIdPerson());

		try {
			person.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		User user = userEditForm.getUser();
		user.setProfile(profile);
		user.setPerson(person);
		user.update();

		return mapping.findForward("showList");

	}

}